package com.huanletao.io;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.event.EventUtils;
import org.junit.Test;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  IOUtils 常用api 方法 示例;
 *
 * </p>
 *
 * @package: com.huanletao.io
 * @description:
 * @className: IODemo1
 * @author: @nncskj admin
 * @date: Created in 2022/5/30
 * @copyright: Copyright (c) 2022/5/30
 * @version: V1.0
 */
public class IOUtilsApiTest {

    /**
     * 转字符串, 关闭流;
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        InputStream in = new URL( "https://commons.apache.org" ).openStream();
        try {
            System.out.println( IOUtils.toString( in ) );

            //按行读取文件内容;
            InputStream is = new FileInputStream("test.txt");
            List<String> lines = IOUtils.readLines(is, "UTF-8");

            // 将行集合写入输出流
            OutputStream os = new FileOutputStream("newTest.txt");
            IOUtils.writeLines(lines, System.lineSeparator(), os, "UTF-8");
        } finally {
            IOUtils.closeQuietly(in);
        }
    }



    public static void main(String[] args) throws IOException {
        File srcFile = new File("src.txt");
        File destFile = new File("dest.txt");
        File srcDir = new File("/srcDir");
        File destDir = new File("/destDir");
// 移动/拷贝文件
        FileUtils.moveFile(srcFile, destFile);
        FileUtils.copyFile(srcFile, destFile);
// 移动/拷贝文件到目录
        FileUtils.moveFileToDirectory(srcFile, destDir, true);
        FileUtils.copyFileToDirectory(srcFile, destDir);
// 移动/拷贝目录
        FileUtils.moveDirectory(srcDir, destDir);
        FileUtils.copyDirectory(srcDir, destDir);
// 拷贝网络资源到文件
        FileUtils.copyURLToFile(new URL("http://xx"), destFile);
// 拷贝流到文件
        FileUtils.copyInputStreamToFile(new FileInputStream("test.txt"), destFile);
    }

    private static void test06() throws IOException {
        File readFile = new File("test.txt");
        // 读取文件
        String str = FileUtils.readFileToString(readFile, "UTF-8");
        // 读取文件为字节数组
        byte[] bytes = FileUtils.readFileToByteArray(readFile);
        // 按行读取文件
        List<String> lines =  FileUtils.readLines(readFile, "UTF-8");

        File writeFile = new File("out.txt");
        // 将字符串写入文件
        FileUtils.writeStringToFile(writeFile, "测试文本", "UTF-8");
        // 将字节数组写入文件
        FileUtils.writeByteArrayToFile(writeFile, bytes);
        // 将字符串列表一行一行写入文件
        FileUtils.writeLines(writeFile, lines, "UTF-8");
    }

    private static void test04() throws IOException {
        // 按照行读取结果
        InputStream is = new FileInputStream("test.txt");
        List<String> lines = IOUtils.readLines(is, "UTF-8");

        // 将行集合写入输出流
        OutputStream os = new FileOutputStream("newTest.txt");
        IOUtils.writeLines(lines, System.lineSeparator(), os, "UTF-8");

        // 拷贝输入流到输出流
        InputStream inputStream = new FileInputStream("src.txt");
        OutputStream outputStream = new FileOutputStream("dest.txt");
        IOUtils.copy(inputStream, outputStream);
    }

    private static void test03() throws IOException {
        // ==== 输入流转换为byte数组 ====
        // 原生写法
        InputStream is = new FileInputStream("foo.txt");
        byte[] buf = new byte[1024];
        int len;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while ((len = is.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
        byte[] result = out.toByteArray();
        // commons写法
        byte[] result2 = IOUtils.toByteArray(is);

      //  IOUtils.write("", FileUtils.writeStringToFile(Paths.get("").toFile()););
    }
}
